import 'package:spendbook/models/model.dart';
import 'package:spendbook/models/user.dart';

class Family extends Model<Family> {
  @override
  String url = '/family';

  User? family;

  String _nickname = '';

  String get nickname {
    return _nickname.isNotEmpty ? _nickname : family!.nickname;
  }

  set nickname(String name) {
    _nickname = name;
  }

  get phone {
    return family?.phone;
  }

  @override
  Map<String, dynamic> toJson() => {
        'family': family?.toJson(),
        'nickname': nickname,
      }..addAll(super.toJson());

  @override
  void fromJson(Map<String, dynamic> json) {
    nickname = json['nickname'];
    family = User()..fromJson(json['family']);
    super.fromJson(json);
  }
}
